package com.ydzc.web.util;

import java.util.List;

/**
 * Created by Administrator on 2017/7/31.
 */
public class Result<T> {
    private int code;
    private String msg;
    private T data;
    private int pageNum;
    private int pageSize;
    private int totalNum;

    //创建 SingleObject 的一个对象
    private static Result result = new Result();

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    private Result() {
    }

    //    public static Result getResult() {
//        return result;
//    }
    public static Result getResult() {
        return new Result();
    }

    public Boolean isSuccess() {
        return this.code != 0 ? true : false;
    }

    public Result successMsg(String msg, T data) {
        this.code = 200;
        this.msg = msg;
        this.data = data;
        return this;
    }

    public Result selectMsg(T data) {
        if (data instanceof List) {
            if (((List) data).size() > 0) {
                this.code = 200;
                this.msg = "查询成功";
                this.data = data;
                return this;
            } else {
                this.code = 0;
                this.msg = "暂无数据";
                this.data = data;
                return this;
            }
        } else {
            if (data != null) {
                this.code = 0;
                this.msg = "返回数据格式有误";
                this.data = data;
                return this;
            } else {
                this.code = 200;
                this.msg = "查询成功";
                this.data = data;
                return this;
            }
        }
    }

    public Result selectMsg(T data, int pageNum, int pageSize, int totalNum) {
        if (data instanceof List) {
            if (((List) data).size() > 0) {
                this.code = 200;
                this.msg = "查询成功";
                this.data = data;
                this.pageNum = pageNum;
                this.pageSize = pageSize;
                this.totalNum = totalNum;
                return this;
            } else {
                this.code = 0;
                this.msg = "暂无数据";
                this.data = data;
                return this;
            }
        } else {
            this.code = 0;
            this.msg = "返回数据格式有误";
            this.data = data;
            return this;
        }
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalNum() {
        return totalNum;
    }

    public Result successMsg(String msg) {
        this.code = 200;
        this.msg = msg;
        this.data = null;
        return this;
    }

    public Result successMsg(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        return this;
    }

    public Result failMsg(String msg, T data) {
        this.code = 0;
        this.msg = msg;
        this.data = data;
        return this;
    }

    public Result failMsg(String msg) {
        this.code = 0;
        this.msg = msg;
        this.data = null;
        return this;
    }

    public Result failMsg(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        return this;
    }

    public Result msg(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        return this;
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
